/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver.listeners;

import wwcontrolserver.datarecorders.AbstractDataRecorder;
import wwcontrolserver.datarecorders.WWControlServerLogger;
import com.phidgets.InterfaceKitPhidget;
import com.phidgets.Phidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.OutputChangeEvent;
import com.phidgets.event.OutputChangeListener;
import java.util.ArrayList;
import java.util.Date;
import org.tmatesoft.sqljet.core.SqlJetException;
import wwcontrolserver.WWControlServer;
/**
 *
 * @author Michael Ferry
 */
public class WWDigitalOutputListener implements OutputChangeListener {

    private WWControlServerLogger WWCSLog;
    private ArrayList<AbstractDataRecorder> DataRecList;

    public WWDigitalOutputListener(WWControlServerLogger WWCSLog, ArrayList<AbstractDataRecorder> DataRecList) {
        this.WWCSLog = WWCSLog;
        this.DataRecList = DataRecList;
    }

    @Override
    public void outputChanged(OutputChangeEvent oce) {
        try {
            Phidget PhidSource = oce.getSource();
            int Index = oce.getIndex();
            boolean state = oce.getState();
            int SerialNumber = PhidSource.getSerialNumber();
            
            
//            //Output the change of the DO state
//            if (SerialNumber == WWControlServer.ifk_HeaterRelay_serial) {
//                if (state) {
//                    WWCSLog.RecordLogEntry("Turning on heater: " + WWControlServer.HeaterSensorToRelayMap[Index] + " at " + new Date());
//                } else {
//                    WWCSLog.RecordLogEntry("Turning off heater: " + WWControlServer.HeaterSensorToRelayMap[Index] + " at " + new Date());
//                }
//
//            } else if (SerialNumber == WWControlServer.ifk_MotorRelay_serial) {
//                if (state) {
//                    WWCSLog.RecordLogEntry("Turning on motor ifk control relay: " + Index + " at " + new Date());
//                } else {
//                    WWCSLog.RecordLogEntry("Turning off motor ifk control relay: " + Index + " at " + new Date());
//                }
//            }
            for (AbstractDataRecorder DataRec : DataRecList) {
                DataRec.InsertDORecord(SerialNumber,Index,state);
            }

//            if (WWDB != null) {
//                try {
//                    int StateInt = state? 1 : 0;
//                    
//                    WWDB.InsertDORecord(SerialNumber, Index, StateInt);
//                } catch (SqlJetException sqle) {
//                    WWCS.RecordLogEntry(sqle.toString());
//                }
//            }
        } catch (PhidgetException phde) {
            WWCSLog.RecordExceptionLogEntry(phde);
        }
    }
}
